package com.newhis.utils;

import com.newhis.properties.MinioProperties;
import com.newhis.thirdpart.CustomMinioClient;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MinioUtils {
    @Autowired
    private static MinioProperties minioProperties;
    public static void createBucket(String bucketName) throws Exception{
        CustomMinioClient minioClient = SpringUtils.getBean(CustomMinioClient.class);
        if (StringUtils.isBlank(bucketName)){
            bucketName = minioProperties.getBucket();
        }
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
        boolean exists = minioClient.bucketExists(bucketExistsArgs);
        if (!exists){
            MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket(bucketName).build();
            minioClient.makeBucket(makeBucketArgs);
        }
    }


}
